package com.ctgu.springshiro.service;

import com.ctgu.springshiro.dao.ItemKillSuccessDao;
import com.ctgu.springshiro.entity.ItemKillSuccess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SchedulerService {

    private static final Logger log = LoggerFactory.getLogger(SchedulerService.class);

    @Autowired
    private ItemKillSuccessDao itemKillSuccessDao;

    @Value("${scheduler.expire.orders.time}")
    private Integer expireTime;

    /**
     * 定时获取status=0的订单并判断是否超过TTL，然后进行失效
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    public void schedulerExpireOrders() {
        try {
            List<ItemKillSuccess> list = itemKillSuccessDao.selectExpireOrders();
            if (list != null && !list.isEmpty()) {
                //java8的写法
                list.stream().forEach(i -> {
                    if (i != null && i.getDiffTime() > expireTime) {
                        itemKillSuccessDao.expireOrder(i.getCode());
                    }
                });
            }
        } catch (Exception e) {
            log.error("定时获取status=0的订单并判断是否超过TTL，然后进行失效-发生异常：", e.fillInStackTrace());
        }
    }

}